<?php

namespace app\libs;

use OSS\Core\OssException;
use OSS\OssClient;
use think\facade\Config;

/**
 * 阿里云oss操作类
 * Class Oss
 * @package app\libs
 */
class Oss
{
    /**
     * 上传文件
     * @param string $folder
     * @return array
     */
    public static function uploadOssFile($folder = '')
    {
        $files = request()->file('files');
        if (empty($files)) {
            $files = request()->file();
        }
        if (!is_array($files)) {
            $files = [$files];
        }
        $fileArr = [];
        if (!empty($files) && count($files) > 0) {
            foreach ($files as $file) {
                $width = null;
                $height = null;
                try {
                    $config = Config::get('system.aliyun_oss');
                    //实例化对象 将配置传入
                    $ossClient = new OssClient($config['accessKeyId'], $config['accessKeySecret'], $config['endpoint']);
                    //这里是有sha1加密 生成文件名 之后连接上后缀
                    $fileName = $folder . '/' . sha1(date('YmdHis', time()) . uniqid()) . '.' . $file->extension();
                    //执行阿里云上传
                    $result = $ossClient->uploadFile($config['bucket'], $fileName, $file);
                    if ($result['info']['http_code'] == '200') {
                        $fileArr[] = ['name' => $fileName, 'url' => $config['url'] . '/' . $fileName];
                    }
                } catch (OssException $e) {
                    return ['code' => 3003, 'msg' => $e->getMessage()];
                }
            }
        } else {
            return ['code' => 3004];
        }
        return ['code' => 1, 'data' => $fileArr];
    }

    public static function moveOssFile($object, $file)
    {
        $back['url'] = '';
        if (empty($file)) {
            $back['code'] = 0;
            $back['msg'] = '文件不能为空';
            return $back;
        }
        $config = Config::get('system.aliyun_oss');
        $web = $config['url'];
        $filetemp = $file;
        if (!file_exists($filetemp)) {
            $back['code'] = 0;
            $back['msg'] = '本地文件不存在';
            return $back;
        }
        //实例化OSS
        try {
            $ossClient = new OssClient($config['accessKeyId'], $config['accessKeySecret'], $config['endpoint']);
            //uploadFile的上传方法
            $ossClient->uploadFile($config['bucket'], $object, $file);
        } catch (OssException $e) {
            //如果出错这里返回报错信息
            $back['code'] = 0;
            $back['msg'] = $e->getMessage();
            return $back;
        }
        $back['code'] = 1;
        $back['url'] = $web . "/" . $object;
        $back['msg'] = '上传成功';
        return $back;
    }

    /**
     * 根据url删除
     * @param $url
     * @return array
     */
    public static function delOssFile($url)
    {
        try {
            $config = Config::get('system.aliyun_oss');
            if (strpos($url, $config['url']) !== false) {
                $urlArr = explode($config['url'] . '/', $url);
                if (isset($urlArr[1])) {
                    $ossClient = new OssClient($config['accessKeyId'], $config['accessKeySecret'], $config['endpoint']);
                    $ossClient->deleteObject($config['bucket'], $urlArr[1]);
                    return ['code' => 1];
                }
            }
            return ['code' => 3006];
        } catch (\Exception $e) {
            return ['code' => 992, 'msg' => $e->getMessage()];
        }
    }
}